package com.yxq.mdt.app.modules.personal.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.modules.personal.ui.fragment.MyPublishFragment;
import com.yxq.mdt.app.modules.personal.ui.fragment.MyPublishReplyFragment;
import com.yxq.mdt.app.modules.socialcontact.adapter.ExamplePagerAdapter;
import com.yxq.mdt.app.modules.socialcontact.ui.fragment.SocialStateFragment;
import com.yxq.mdt.app.utils.MediaPlayerHelper;
import com.yxq.mdt.app.utils.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import cn.jzvd.Jzvd;

/**
 * 我的发表
 */
public class MainPersonalMyPublishAc extends BaseActivity {

    private static final String[] CHANNELS = new String[]{"我的帖子", "我的回帖"};
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private List<String> mDataList = Arrays.asList(CHANNELS);
    private ExamplePagerAdapter mExamplePagerAdapter;
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private FragmentContainerHelper mFragmentContainerHelper = new FragmentContainerHelper();
    MyPublishFragment myPublishFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_personal_my_publish;
    }

    @Override
    public void initView() {
        initFragments();
        mExamplePagerAdapter = new ExamplePagerAdapter(getSupportFragmentManager(), mFragments, mDataList);
        viewPager.setAdapter(mExamplePagerAdapter);
        mFragmentContainerHelper.handlePageSelected(0, false);
        initMagicIndicator4();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    /**
     * 初始化标题栏
     */
    private void initMagicIndicator4() {
        CommonNavigator commonNavigator = new CommonNavigator(mActivity);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context); //使用自定义
                simplePagerTitleView.setNormalColor(Color.BLACK);
                simplePagerTitleView.setSelectedColor(Color.BLACK);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setTextSize(19);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                linePagerIndicator.setLineWidth(UIUtil.dip2px(context, 80));
                linePagerIndicator.setLineHeight(UIUtil.dip2px(context, 2));
                linePagerIndicator.setColors(Color.parseColor("#FD4343"));
                return linePagerIndicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return UIUtil.dip2px(mActivity, 0);
            }
        });
        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(0.1f));
        fragmentContainerHelper.setDuration(0);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
            }
        });
        viewPager.setOffscreenPageLimit(5);

    }

    /**
     * 初始化fragment
     */
    private void initFragments() {

        //我的帖子
        myPublishFragment = new MyPublishFragment();
        Bundle myPublishBundle = new Bundle();
        myPublishBundle.putString(SocialStateFragment.EXTRA_TEXT, CHANNELS[0]);
        myPublishFragment.setArguments(myPublishBundle);
        mFragments.add(myPublishFragment);

        //我的回帖
        MyPublishReplyFragment myPublishReplyFragment = new MyPublishReplyFragment();
        Bundle myPublishReplyBundle = new Bundle();
        myPublishReplyBundle.putString(SocialStateFragment.EXTRA_TEXT, CHANNELS[1]);
        myPublishReplyFragment.setArguments(myPublishReplyBundle);
        mFragments.add(myPublishReplyFragment);

    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.goOnPlayOnPause();
        MediaPlayerHelper.realese();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }
}
